package com.opengms.HydroModelsIntegration.Dao;

import com.opengms.HydroModelsIntegration.Domain.InpData;
import com.opengms.HydroModelsIntegration.Domain.TreeNode;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @Author: David.Xiao
 * @Date: 2019/1/7 20:03
 * @Description:
 */
public class TreeDataMaker {

    private LinkedHashMap<String,ArrayList<String>> nodeMapList;

    public LinkedHashMap<String, ArrayList<String>> getNodeMapList() {
        return nodeMapList;
    }
    public void setNodeMapList(LinkedHashMap<String, ArrayList<String>> nodeMapList) {
        this.nodeMapList = nodeMapList;
    }

    private List<TreeNode> treeNodeList;

    public TreeDataMaker(InpData inpData,JSONArray oldTree)
    {
        this.newTree = oldTree;
        this.nodeMapList=getMainList(inpData);

    }

    /**
     * 制作新的JSON树
     * @param oldTree
     * @return
     */
    public JSONArray makeTreeData(JSONArray oldTree)
    {
        JSONArray newTree = oldTree;
        analysisJson(newTree);
        return newTree;
    }

    /**
     * 解析JSON树，并迭代地加入子节点
     * @param object
     */
    public void analysisJson(Object object)
    {
        if(object instanceof JSONArray)
        {
            JSONArray jsonArray = (JSONArray) object;
            for(int i = 0;i<(jsonArray).size();i++)
            {
                analysisJson(jsonArray.get(i));

            }
        }
        else if(object instanceof JSONObject)
        {
            JSONObject jsonObject = (JSONObject) object;
            Iterator iterator = jsonObject.keys();
            while (iterator.hasNext())
            {
                String key = iterator.next().toString();
                Object obj = jsonObject.get(key);
                if(obj instanceof JSONArray)
                {
                    analysisJson((JSONArray)obj);
                }
                else if(obj instanceof JSONObject)
                {
                    analysisJson((JSONObject)obj);
                }
                else
                {
//                    System.out.println("正在遍历："+key+"-->"+obj.toString());
                }
            }
            String mapKey;
            ArrayList<TreeNode> list = new ArrayList<>();
            if(nodeMapList.containsKey(mapKey =((jsonObject).get("text").toString())))
            {
                for (String str: nodeMapList.get(mapKey)) {
                    TreeNode treeNode = genNode(str,mapKey+"#"+str);
                    System.out.println("href："+mapKey+"#"+str);
                    list.add(treeNode);
                }
                (jsonObject).put("nodes",JSONArray.fromObject(list));
            }

        }
    }

    /**
     * 获取要加入JSON树的子节点的内容
     * @param inpData
     * @return nodeMapList
     */
    private LinkedHashMap<String,ArrayList<String>> getMainList(InpData inpData)
    {
        this.nodeMapList = new LinkedHashMap<>();
        ArrayList<String> titleList = new ArrayList<>();
        ArrayList<String> optionList = new ArrayList<>(Arrays.asList("General","Dates","TimeSteps","DynamicWave","InterfaceFiles","Reporting"));
        ArrayList<String> climatologyList = new ArrayList<>(Arrays.asList("Temperature","Evaporation","WindSpeed","SnowMelt","ArealDepletion","Adjustments"));

        ArrayList<String> rainGageList = new ArrayList<>();
        ArrayList<String> subcatchmentList = new ArrayList<>();
        ArrayList<String> junctionsList = new ArrayList<>();
        ArrayList<String> outfallsList = new ArrayList<>();
        ArrayList<String> storageUnitsList = new ArrayList<>();
        ArrayList<String> conduitsList = new ArrayList<>();
        for (String title:inpData.getTitleArrayList()) {
            titleList.add(title);
        }
        for (InpData.Raingage raingage:inpData.getRaingageArrayList()) {
            rainGageList.add(raingage.getGage());
        }
        for (InpData.Subcatchment subcatchment:inpData.getSubcatchmentArrayList()) {
            subcatchmentList.add(subcatchment.getSubcatchment());
        }
        for (InpData.Junction junction:inpData.getJunctionArrayList()) {
            junctionsList.add(junction.getJunction());
        }
        for (InpData.Outfall outfall:inpData.getOutfallArrayList()) {
            outfallsList.add(outfall.getOutfall());
        }
        for (InpData.Storage storage:inpData.getStorageArrayList()) {
            storageUnitsList.add(storage.getStorage());
        }
        for (InpData.Conduit conduit:inpData.getConduitArrayList()) {
            conduitsList.add(conduit.getConduit());
        }

        nodeMapList.put("TitleNotes",titleList);
        nodeMapList.put("Options",optionList);
        nodeMapList.put("Climatology",climatologyList);
        nodeMapList.put("RainGages",rainGageList);
        nodeMapList.put("Subcatchments",subcatchmentList);
        nodeMapList.put("Junctions",junctionsList);
        nodeMapList.put("Outfalls",outfallsList);
        nodeMapList.put("StorageUnits",storageUnitsList);
        nodeMapList.put("Conduits",conduitsList);
        return nodeMapList;
    }



    private JSONArray newTree;



    public TreeNode genNode(String text,String href)
    {
        TreeNode treeNode = new TreeNode();
        treeNode.setText(text);
        treeNode.setHref(href);
        return treeNode;
    }

}
